package com.hbt.orderfood.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.utils.StringUtils;

import java.util.List;

public class MngFoodAdapter extends RecyclerView.Adapter {
    public List<Food> getFoods() {
        return foods;
    }

    public void setFoods(List<Food> foods) {
        this.foods = foods;
    }

    private List<Food> foods;
    public MngFoodAdapter(List<Food> foods){
        this.foods = foods;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(params);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mng_food_item,layout,true);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder hold = (ViewHolder)holder;
        hold.tv_name.setText(foods.get(position).getName());
        hold.tv_price.setText(String.valueOf(foods.get(position).getPrice())+"￥");
        StringUtils.setImg(hold.img, foods.get(position).getType());
        hold.food_item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.itemClick(foods.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return foods.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout food_item_layout;
        TextView tv_name;
        TextView tv_price;
        ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            food_item_layout = itemView.findViewById(R.id.food_item_layout);
            tv_name = itemView.findViewById(R.id.name);
            tv_price = itemView.findViewById(R.id.price);
            img = itemView.findViewById(R.id.img);
        }
    }

    public interface ItemClickListener{
        void itemClick(Food food);
    };

    public void setClickListener(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    private ItemClickListener clickListener;


}
